package com.thomas.framework.service.impl;

import com.thomas.framework.core.AppException;
import com.thomas.framework.core.ServiceBase;
import com.thomas.framework.dao.ISysRoleDao;
import com.thomas.framework.entity.SysModel;
import com.thomas.framework.entity.SysRole;
import com.thomas.framework.entity.SysUser;
import com.thomas.framework.service.ISysModelService;
import com.thomas.framework.service.ISysRoleService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by taohj on 2014/4/17.
 */

@Service
public class SysRoleServiceImpl extends ServiceBase<SysRole> implements ISysRoleService{


    @Autowired
    private ISysRoleDao dao;


    @Autowired
    private ISysModelService modelService;




    @Override
    public  boolean create(SysRole role){

        SysRole validatarole=new SysRole();
        validatarole.setName(role.getName());
        validatarole=dao.select(validatarole);
        if (validatarole!=null){
            throw  new AppException("There has same name,please type othere");
        }

        role.setId(CommonUtil.getUUID());
        return  dao.insert(role)>0;
    }

    @Override
    public  boolean remove(SysRole role){
        List<SysModel> models=modelService.getFunctionMenuBySysRole(role);
        if (models!=null && models.size()>0){
            throw  new AppException("Please cancel the permission for the role");
        }



        dao.authorizeDelete(role);
        return  dao.delete(role)>0;
    }

    @Override
    public boolean modify(SysRole role){
        SysRole validatarole=new SysRole();
        validatarole.setName(role.getName());
        validatarole=dao.select(validatarole);
        if (validatarole!=null && !(validatarole.getId().equals(role.getId()))){
            throw  new AppException("There has same name,please type othere");
        }

        return  dao.update(role)>0;
    }


    @Override
    public  SysRole   getById(SysRole role){

        return  dao.selectById(role);
    }

    @Override
    public List<SysRole> getList(SysRole sysRole) {
        return dao.selectList(sysRole);
    }


    @Override
    public PageResult<SysRole> getByPage(PageResult<SysRole> pageResult){

        return  pageResult.setRows(dao.selectbyPage(pageResult));
    }

    @Override
    public SysRole get(SysRole sysRole) {
        return dao.select(sysRole);
    }

    public boolean authorize(SysUser user,SysRole role) {



        if (role==null){
            throw  new AppException("role is null");
        }

        dao.authorizeDelete(role);


        if (role.getModels()==null){
            throw new AppException("role.models is null");
        }
        if (!(role.getModels().size()>0)){
            return true;
        }
        Map<String,Object>  params=new HashMap<String, Object>();
        params.put("user",user);
        params.put("role",role);
        params.put("createTime",new Date());
/*
        if (true){
            throw new AppException("事物测试异常抛出");
        }*/

        return dao.authorize(params);
    }

    public List<SysRole> getRolesByUser(SysUser user) {
        List<SysRole>  roles=this.getList(null);
        user.setRoles(dao.selectByUser(user));
        for (int i=0;i<roles.size();i++){
            SysRole role=roles.get(i);
            for (SysRole r: user.getRoles()){
                 if (role.getId().equals(r.getId())){
                     role.setChecked(true);
                     break;
                 }
            }
        }

        return  roles;
    }
}
